// 使用all实现allSettled
function allSettled(promises) {
  return Promise.all(promises.map(p =>
    Promise.resolve(p)
      .then(value => ({
        status: 'fulfilled',
        value: value
      }))
      .catch(reason => ({
        status: 'rejected',
        reason: reason
      }))
  ));
}
// 手写实现promise
function allSettled2(promises) {
  let result = new Array(promises.length)
  let doneCount = 0
  return new Promise((resolve, reject) => {
    for (let i = 0; i < promises.length; i++) {
      Promise.resolve(promises).then(res => {
        result[i] = {
          value: res,
          status: 'fulfilled'
        }
        doneCount++
        if (doneCount === promises.length) resolve(result)
      }).catch(err => {
        result[i] = {
          value: err,
          status: 'fulfilled'
        }
        doneCount++
        if (doneCount === promises.length) resolve(result)
      })
    }
  })
}
let p1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 1000)
})
let p2 = Promise.reject('我拒绝了')
let p3 = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject('超时拒绝')
  }, 3000)
})
let allSettledP = allSettled([p1, p2, p3])
allSettledP.then(res => {
  console.log(res);
})

